//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var ratio = 1;

var time = 0;
var dtime = 0;
var startime = 0;
var lasttime = 0;
var g_infoShow = new infoShow();
var bClear = false;
var clearTime = 0;


var mouse = {x: 0, y: 0, x0: 0, y0: 0, dx: 0, dy: 0, down: false, left: false, right: false};

var lineColor = 'rgba(255, 255, 225, 1)';

function canvasResize()
{

	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;

	if(!g2D)g2D = canvas.getContext("2d");


}




function init()
{
	canvas = document.getElementById('canvas3D');

	canvasResize();

	var touchable = 'createTouch' in document;

	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);
	}

	canvas.addEventListener( 'contextmenu', function ( event )
	{
		event.preventDefault();
	}, false );

}

var bTimeout = false;
window.onresize = function()
{
	if (fullWindow) canvasResize();

	bTimeout = true;
}

var first = true;
var lastOrien;

window.ondeviceorientation =  function(e)
{
	if(first)
	{
		lastOrien = window.orientation;
		first = false;
	}

	if(window.orientation !== lastOrien)
	{
		window.onresize();
		lastOrien = window.orientation;
	}

	g_infoShow.add("alpa: ", e.alpha);
	g_infoShow.add("beta: ",e.beta);
	g_infoShow.add("gamma: ",e.gamma);
	g_infoShow.add("orientation: ",window.orientation);
}

window.onblur = function()
{
	bTimeout = true;
}

function drawScene()
{
	g2D.fillStyle = rgba(3, 18, 35, 1);
	g2D.fillRect(0, 0, width, height);


	tunnel(g2D);

	g_infoShow.add("dTime:  ", dtime);
	//g_infoShow.show();

}



function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();


	if(event.clientX) // on mouse down
	{
		mouse.x = mouse.x0 = event.clientX;
		mouse.y = mouse.y0 = event.clientY;

		vangle = 0;
		mouse.down = true;
	}

	if(!event.touches) return;

	// on touch star

	for(var i = 0; i < event.touches.length; i++)
	{
		mouse.x = mouse.x0 = event.touches[0].clientX;
		mouse.y = mouse.y0 = event.touches[0].clientY;

		vangle = 0;
		mouse.down = true;
	}

}

function onTouchMove(event)
{
	event.preventDefault();

	if(event.clientX && mouse.down) // on mouse move
	{
		mouse.dx = event.clientX - mouse.x0;
		mouse.dy = event.clientY - mouse.y0;
		mouse.x0 = mouse.x = event.clientX;
		mouse.y0 = mouse.y = event.clientY;

	}

	if(!event.touches) return;

	for(var i = 0; i < event.touches.length; i++)
	{
		mouse.dx = event.touches[0].clientX - mouse.x0;
		mouse.dy = event.touches[0].clientY - mouse.y0;
		mouse.x0 = mouse.x = event.touches[0].clientX;
		mouse.y0 = mouse.y = event.touches[0].clientY;
	}


}

function onTouchEnd(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse up
	{
		mouse.dx = 0;
		mouse.dy = 0;

		if(event.button === 2 || event.button === 3)mouse.right = true;
		else mouse.left = true;

		mouse.down = false;

	}

	if(!event.touches) return;

	mouse.dx = 0;
	mouse.dy = 0;

	mouse.down = false;

	for(var i = 0; i < event.touches.length; i++)
	{


	}

}

function gameloop()
{
	window.requestAnimFrame( gameloop );

	if(bTimeout)
	{
		lasttime = Date.now();
		bTimeout = false;
	}

	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();

}


function game()
{
	init();
	gameloop();
}




